package com.xiaozhen.mqtt.handler;

import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.codec.MqttTopicSubscription;
import net.dreamlu.iot.mqtt.core.server.IMqttServerAuthHandler;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用于客户端认证
 *
 * @author zhenweisheng
 * @date 2021/10/11 13:45
 * @description:
 */
@Service
@Slf4j
public class MyMqttServerAuthHandler implements IMqttServerAuthHandler {

    @Override
    public boolean authenticate(String clientId, String userName, String password) {
        // 客户端认证逻辑实现
        log.info("设备{}，用户名{}，密码{}", clientId, userName, password);
        return true;
    }

    @Override
    public boolean isValidSubscribe(List<MqttTopicSubscription> topicSubscriptionList) {
        // 校验客户端订阅的 topic，校验成功返回 true，失败返回 false
        for (MqttTopicSubscription subscription : topicSubscriptionList) {
            String topicName = subscription.topicName();
            MqttQoS mqttQoS = subscription.qualityOfService();
        }
        return true;
    }
}
